<?php
namespace Admin\Controller;
use Think\Controller;
class ManageziController extends Controller {
   public function _initialize()  
    {  
		header("Content-Type: text/html; charset=utf-8");

    	if(!session('user')&&!session('zi_user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));die;
    	}
    } 
	public function index(){
		echo 'ok';	
	}
	public function nav_qudao(){
		$this->display('gongzhong_nav');
	}
	public function add_zi_qudao(){
		$this->display();
	} 
	public function add_zizhanghao(){
		$this->display();
	} 
	public function manage_zi_qudao(){


		$this->display();
	} 
	/**
	 * 账号冻结ajax冻结账号处理
	 * @return [type] [description]
	 */
	public function frozen(){
		if(IS_POST && IS_AJAX){
			$where_web['web_id']=$_POST['web_id'];
			$res_temp=M("web_user")->where($where_web)->find();
			if($res_temp['is_frozen']==1){
				$data['is_frozen']=0;
				$res=M('web_user')->where($where_web)->save($data);
				if($res){
					$msg['code']=1;
					$msg['message']='修改成功';
					$msg['old_type']=1;
					/**
					* 解除子账号绑定
					*/
					$where['web_id']=$_POST['web_id'];
					$bind_id=M('web_user')->where($where)->getField('bind_id');
					M('wechat_bind')->delete($bind_id);
				}
				else{
					$msg['code']=2;
					$msg['message']='修改失败';
				}
			}
			elseif ($res_temp['is_frozen']==0) {
				$data['is_frozen']=1;
				$res=M('web_user')->where($where_web)->save($data);
				if($res){
					$msg['code']=1;
					$msg['message']='修改成功';
					$msg['old_type']=0;
					/**
					* 解除子账号绑定
					*/
					$where['web_id']=$_POST['web_id'];
					$bind_id=M('web_user')->where($where)->getField('bind_id');
					M('wechat_bind')->delete($bind_id);
				}
				else{
					$msg['code']=2;
					$msg['message']='修改失败';
				}
			}
			else{
				$msg['code']=3;
				$msg['message']='用户失效';
			}
		}
		else{
			$msg['code']=4;
			$msg['message']='提交数据错误';
		}
		$this->ajaxReturn($msg);
	}
	public function manage_zizhanghao(){

		$data = M('user');
		if($_POST){
			/**
			 *
			 *删除处理
			 * 
			 */
            foreach ($_POST['id_arr'] as $key => $value) {
                $del_arr=$value.$del_arr;
                if($_POST['id_arr'][$key+1]){
                    $del_arr=','.$del_arr;
                }
            }
            $res=$data->delete($del_arr);
            if($res){
                $this->success('删除成功',U('manage_zizhanghao'),1);
            }
            else{
                $this->error('删除失败',U('manage_zizhanghao'),1);
            }
		}
		else{
			/**
			 *
			 * 
			 * 分页处理
			 *
			 * 
			 */
			if(!$_GET['p'])$_get['p']=1;
			$where['u_type']=3;//类型
			$where['u_pro_id']=$_SESSION['zi_user']['u_pro_id'];
			$where['u_city_id']=$_SESSION['zi_user']['u_city_id'];
			$where['p_id']=$_SESSION['zi_user']['u_id'];
			$count      = $data->where($where)->count();
			$Page       = new \Think\Page($count,10);
			$show       = $Page->show();
			$list = $data->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('count',$count);
			$this->assign('list',$list);
			$this->assign('page',$show);
		    $this->display();
		}


	} 
	public function next_m(){
		$where['u_id']=$_GET['u'];
		$user_data=M('user')->where($where)->find();
		session('user',$user_data);
		$result=D("Wxauthorise")->manage_me();
		$this->assign('count',$result['count']);
		$this->assign('list',$result['list']);
		$this->assign('page',$result['show']);
		$this->display('Manageme/manage_me');	
	}
	/**
	 * 添加子账号头像
	 * 
	 */
	public function ajax_img(){
		$res=D('User')->upload_img('login');
		$this->ajaxReturn($res);
	}

	/**
	 * 获取按揭员列表子账号功能
	 */
	public function get_aj_data_list(){
		if(IS_AJAX&&IS_POST){
			$web_id=$_POST['web_id'];
			$where['web_id']=array('neq',$web_id);
			$where['p_u_id']=$_SESSION['user']['u_id'];
			$where['web_type']=1;
			$res_data_ajax=M('web_user')->where($where)->select();
			$msg['code']=1;
			$msg['message']='数据捕获成功';
			$msg['data']=$res_data_ajax;
		}
		else{
			$msg['code']=2;
			$msg['message']='请求方式错误';
		}

		$this->ajaxReturn($msg);
	}
	/**
	 * 按揭员数据转移
	 */
	public function change_aj_pow(){
		if(IS_AJAX&&IS_POST){
			/********************消息来源表处理***********************/
			$old_web_id=$_POST['old_web_id'];
			$new_web_id=$_POST['new_web_id'];
			$where['from_user_group']=array('LIKE',$old_web_id.'%');
			$old_web_id_data=M('from_user_group')->where($where)->select();
			$new_web_id_data=$old_web_id_data;
			$i=0;
			foreach ($old_web_id_data as $key => $value) {
				  $new_from_group_data[$key]=str_replace($old_web_id, $new_web_id, $old_web_id_data[$key]['from_user_group']);
				  $new_web_id_data[$key]['from_user_group']=$new_from_group_data[$key];
				  $re=M('from_user_group')->data($new_web_id_data[$key])->save();
				  if($re){$i++;}
			}
			/********************消息来源表处理***********************/
			/********************聊天室表处理***********************/
			$k=0;
			 $old_chat_data=M('chat')->where('first_user='.$old_web_id)->select();
			 $new_chat_data=$old_chat_data;
			 foreach ($old_chat_data as $key => $value) {
			 	  $new_chat_data[$key]['first_user']=$new_web_id;
				  $reo=M('chat')->data($new_chat_data[$key])->save();
				  if($reo){$k++;}
			 }
			/********************聊天室表处理***********************/
			$msg['code']=1;
			$msg['message']='转移成功';
			$msg['data']=$k;

		}
		else{
			$msg['code']=2;
			$msg['message']='请求方式错误';
		}

		$this->ajaxReturn($msg);
	}

}
